package MysqlLogin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
* 连接数据库
* */
public class ConnectionMySQL {
    private static String username = "1emontree";
    private static String password = "qq18224072177";
    private static String url = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true";
    private static Connection connection = null;
    public static Connection getConnection(){
        try{
            try {
                Class.forName("com.mysql.jdbc.Driver");
            }catch (Exception e){
                e.printStackTrace();
            }
            connection = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            System.out.println("数据库连接异常");
            e.printStackTrace();
        }
        return connection;
    }
    public static void closeConnection(){
        if(connection!=null){
            try {
                //关闭数据库连接
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
